package homework.Day0322;

import java.util.ArrayList;
import java.util.Scanner;

public class CabinetSimulation {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        boolean b = true;
        ArrayList<String> cabinet = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            cabinet.add("X");
        }
        while (b) {
            System.out.println("======================Locker======================");
//                            "  (1)[X]  (2)[X]  (3)[X]  (4)[X]  (5)[X]\n" +
//                            "  (6)[X]  (7)[X]  (8)[X]  (9)[X] (10)[X]\n"
            for (int i = 0; i < cabinet.size(); i++) {
                System.out.printf("(%d)[%s]  ",i + 1,cabinet.get(i));
                if ((i + 1) % 5 == 0){
                    System.out.println();
                }
                if ((i +1) == cabinet.size() && (i + 1) % 5 != 0){
                    System.out.print("\n");
                }
            }
            System.out.println("======================Locker======================\n" +
                            "1. 添加一个格子\n" +
                            "2. 删除一个格子\n" +
                            "3. 打开一个格子\n" +
                            "4. 关闭一个格子\n" +
                            "5. 退出");
            System.out.print("你的输入：");
            String input = in.nextLine();
            try {
                int num = Integer.parseInt(input);
                switch (num){
                    case 1:
                        cabinet.add("X");
                        break;
                    case 2:
                        System.out.print("请输入所删格子的代号:");
                        int codename = 0;
                        try {
                            codename = in.nextInt();
                            if (codename > 0 && codename < cabinet.size()) {
                                cabinet.remove(codename - 1);
                            } else {
                                System.out.println("你输的格子不存在");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case 3:
                        System.out.print("请输入所打开的格子的代号:");
                        try {
                            int open = in.nextInt();
                            if (open > 0 && open < cabinet.size()) {
                                cabinet.set(open - 1, "O");
                            } else {
                                System.out.println("你输的格子不存在");
                            }
                        } catch (Exception e) {
                         e.printStackTrace();
                        }
                        break;
                    case 4:
                        System.out.print("请输入所关闭的格子的代号:");
                        int close = in.nextInt();
                        try {
                            if (close > 0 && close < cabinet.size()) {
                                if ("O".equals(cabinet.get(close - 1))) {
                                    cabinet.set(close - 1, "X");
                                } else {
                                    System.out.println("这个格子已打开");
                                }
                            } else {
                                System.out.println("你输的格子不存在");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case 5:
                        b = false;
                        break;
                    default:
                        System.out.println("输入格式错误");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入格式错误");
            }

        }

    }

}
